/**
 * 
 */
package calculation;

/**
 * @author Michael
 * Determine whether an integer is a palindrome. 
 * Do this without extra space.
 */
public class PalindromeNumber {

	
	// potential overflow
	public static boolean isPalindromeByReversingNumber(int n) {
		if (n < 0)
			return false;
		
		int p = 0;
		int m = n;
		if (m > 0) {
			p = p * 10 + m % 10;
			m /= 10;
		}
		
		return p == n;
	}
	
	public static boolean isPalindromeByComparingEnds(int n) {
		if (n < 0)
			return false;
		int m = n;
		int divisor = 1;
		while (m >= 10) {
			m /= 10;
			divisor *= 10;
		}
		
		m = n;
		while (m >= 10) {
			if (m / divisor == m % 10) {
				m = (m % divisor) / 10;
				divisor /= 100;
			}
			else
				return false;
		}
		
		return true;
	}
	
	boolean PalindromeInt(int num) {
		  if (num < 0 || (num % 10 == 0 && num != 0))
		    return false;

		  int reverse = 0;
		  while (num >= reverse) {
		    if (reverse == num || reverse == num / 10)
		      return true;
		    reverse = reverse * 10 + num % 10;
		    num /= 10;
		  }

		  return false;
	}
	
	/**
	 * 
	 */
	public PalindromeNumber() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		for (int i = 0; i <= 99999; i++)
			if (isPalindromeByComparingEnds(i))
				System.out.println(i);

	}

}
